/**
 * https://www.acwing.com/problem/content/4961/
 */
package 题目集.动态规划.最长递增子序列;

import java.util.Scanner;

public class ch06_接龙序列 {
    static int n;
    static int maxN= (int) (1e5+1);
    static int[] arr;
    static int[] ends=new int[10];  //记录以对应下标结尾的上一个最长上升子序列的长度
    static int cnt;

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        arr=new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=sc.nextInt();
        }
        for (int i = 0; i < arr.length; i++) {
            int h = h(arr[i]);
            int t = arr[i] % 10;
            int pre=ends[h];
            if (ends[t]<pre+1){
                ends[t]=pre+1;
            }
            cnt=Math.max(ends[t],cnt);
        }
        System.out.println(n-cnt);
    }
    public static int h(int n){
        int p = (int) Math.log10(n);
        double pow = Math.pow(10, p);
        return (int) (n/pow);
    }
}
